package controllers

import (
	"blogs/models"
	"blogs/utils"
	beego "github.com/beego/beego/v2/server/web"
	"strconv"
)

// Operations about object
type NavController struct {
	beego.Controller
}

// @Title Get
// @Description 通过id查询文章
// @Param	objectId		path 	string	true		"the objectid you want to get"
// @Success 200 {object} models.Object
// @Failure 403 :objectId is empty
// @router /:objectId [get]
func (o *NavController) Get() {
	objectId := o.Ctx.Input.Param(":objectId")

	// page 当前页
	page, _ := o.GetInt("page")
	if page == 0 {
		page = 1
	}
	// 条数
	pageSize, _ := o.GetInt("pageSize")
	if pageSize == 0 {
		pageSize = 5
	}
	// 总条数
	var total int64
	utils.DB.Table("article").Where("nav_id=?", objectId).Count(&total)

	object, _ := strconv.Atoi(objectId)
	nav := models.Nav{}
	utils.DB.Preload("Article").Where("id=?", object+1).Offset((page - 1) * pageSize).Limit(pageSize).Find(&nav)
	o.Data["json"] = map[string]interface{}{
		"msg":  "查询成功",
		"data": nav,
		"code": false,
		"page": map[string]interface{}{
			"page":     page,
			"pageSize": pageSize,
			"count":    total,
		},
	}
	o.ServeJSON()
}

// @Title GetAll
// @Description 查询所有导航
// @Success 200 {object} models.Article
// @Failure 403 :objectId is empty
// @router / [get]
func (o *NavController) GetAll() {

	nav := []models.Nav{}
	utils.DB.Find(&nav)

	o.Data["json"] = map[string]interface{}{
		"msg":  "查询成功",
		"data": nav,
		"code": false,
	}
	o.ServeJSON()
}
